package com.mhc.suzuki.service.impl;

import com.mhc.haval.api.dto.AuditQuotaDTO;
import com.mhc.haval.api.service.IAuditQuotaService;
import com.mhc.haval.security.model.User;
import com.mhc.suzuki.bo.GpsInfoBO;
import com.mhc.suzuki.bo.OrderAuditApplyBO;
import com.mhc.suzuki.bo.SubmitTaskBO;
import com.mhc.suzuki.constant.*;
import com.mhc.suzuki.dal.mapper.ext.OrderAuditApplyExtMapper;
import com.mhc.suzuki.dal.model.*;
import com.mhc.suzuki.dal.query.CarInfoQuery;
import com.mhc.suzuki.dal.query.OrderAuditApplyQuery;
import com.mhc.suzuki.dal.query.OrderQuery;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.manager.*;
import com.mhc.suzuki.service.*;
import com.mhc.suzuki.vo.WanderResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 各角色提交方法
 * Created by HMJ on 2017/9/14.
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SubmitTaskServiceImpl implements SubmitTaskService {

    @Autowired
    private ApplyService applyService;
    @Autowired
    private CarInfoManager carInfoManager;
    @Autowired
    private OrderManager orderManager;
    @Autowired
    private OrderAuditApplyManager orderAuditApplyManager;
    @Autowired
    private OrderAuditApplyExtMapper orderAuditApplyExtMapper;
    @Autowired
    private OrderAuditTaskService orderAuditTaskService;
    @Autowired
    private PaymentRequestService paymentRequestService;
    @Autowired
    private LoanInfoManager loanInfoManager;
    @Autowired
    private ModifyService modifyService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PaymentNotificationService paymentNotificationService;
    @Autowired
    private IAuditQuotaService iAuditQuotaService;
    @Autowired
    private OrderAuditTaskManager orderAuditTaskManager;

    /**
     * 保存GPS确认字段
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean saveGPSInfo(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**是否存在安装GPS确认字段，如果存在，则更新车辆GPS信息**/
        if (submitTaskBO.getNeedGps() != null) {
            /**根据订单号更新车辆信息**/
            log.info("正在写入GPS确认信息...");
            CarInfoQuery carInfoQuery = new CarInfoQuery();
            CarInfoDO carInfoRecord = new CarInfoDO();
            carInfoQuery.createCriteria().andOrderNoEqualTo(submitTaskBO.getOrderNo());
            carInfoRecord.setNeedGps(submitTaskBO.getNeedGps());
            carInfoManager.updateByQuerySelective(carInfoRecord, carInfoQuery);
            log.info("写入成功...");
            return true;
        }
        return false;
    }

    /**
     * 业务经理--是否显示GPS确认信息
     *
     * @param orderNo
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean isShowGPS(String orderNo, User logedUser) throws SuzukiCommonException {
        log.info("校验当前用户...");
        if (!RoleTypeEnum.ROLE_SALESMANAGER.getCode().equals(logedUser.getRoleList().get(0).getRoleCode())) {
            throw new SuzukiCommonException(ErrorCodeEnum.NO_AUTH);  //没有权限
        }
        /**1、查询当前订单的申请类型**/
        OrderAuditApplyQuery orderAuditApplyQuery = new OrderAuditApplyQuery();
        //查询正常的状态
        orderAuditApplyQuery.createCriteria().andOrderNoEqualTo(orderNo).andApplyStatusEqualTo(ApplyStatusEnum.NORMAL.getCode());
        List<OrderAuditApplyDO> orderAuditApplyList = orderAuditApplyManager.selectByQuery(orderAuditApplyQuery);
        if (!CollectionUtils.isEmpty(orderAuditApplyList)) {
            /**获取订单的申请类型**/
            int applyType = orderAuditApplyList.get(0).getApplyType();
            if (ApplyTypeEnum.ACCOM_APPLY.getCode().equals(applyType)) {
                /**通融申请,判断额度内还是额度外**/
                log.info("正在获取贷款金额...");
                LoanInfoDO loanInfo = loanInfoManager.selectByOrderNo(orderNo);
                BigDecimal loanAmonut = loanInfo.getLoanAmount();
                log.info("贷款金额为{}", loanAmonut);
                log.info("正在获取个人额度范围...");
                AuditQuotaDTO auditQuotaInfo = iAuditQuotaService.getAuditQuotaDetailByStaffId(logedUser.getStaffId());
                BigDecimal quotaAmount = auditQuotaInfo.getMaxQuotaAmount();
                log.info("当前额度为{}", quotaAmount);
                log.info("正在校验符合的额度范围...");
                if (loanAmonut.compareTo(quotaAmount) > 0) {
                    /**额度外,不显示**/
                    log.info("贷款金额超出当前业务经理的额度范围...");
                    return false;
                }
                log.info("贷款金额在当前业务经理的额度范围内...");
            }
        }
        return true;
    }

    /**
     * 审核员提交审核
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean auditorSubmitTask(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        log.info("当前为{}提交审核阶段...", logedUser.getRoleList().get(0).getRoleName());
        return applyService.saveOrderAudit(submitTaskBO, logedUser);
    }


    /**
     * 业务员撤销申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean salesManRevokeTask(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**查询该订单是否符合发起业务员撤销申请的要求**/
        int orderStatus = orderService.selectOrderStatus(submitTaskBO.getOrderNo());
        /**订单处于审核中才能发起撤销申请动作**/
        if (OrderStatusEnum.UNDER_REVIEW.getCode().equals(orderStatus)) {
            Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.WITHDRAW.getCode());
            if (delFlag) {
                /**订单重新发起后，更新订单状态**/
                orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode());
                return true;
            }
            //撤回申请错误
            throw new SuzukiCommonException(ErrorCodeEnum.WITHDRAW_ERROR);
        }
        //订单状态无效，无法发起申请
        throw new SuzukiCommonException(ErrorCodeEnum.APPLYSTATUSERROR);
    }

    /**
     * 业务员作废申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean invalidApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.INVALID.getCode());
        if (delFlag) {
            /**订单作废后，更新订单状态**/
            orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.CANCELLED.getCode());
            return true;
        }
        //作废申请错误
        throw new SuzukiCommonException(ErrorCodeEnum.CANCEL_ERROR);
    }


    /**
     * 业务员发起贷款申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean salesManLoanApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**1、查询订单状态**/
        int orderStatus = orderService.selectOrderStatus(submitTaskBO.getOrderNo());
        /**2、校验是否录入了垫款信息**/
        log.info("正在校验垫款信息...");
        LoanInfoDO loanInfo = loanInfoManager.selectByOrderNo(submitTaskBO.getOrderNo());
        if (loanInfo.getPayeeDealerId() == null) {
            //垫款信息为空，无法发起贷款申请
            throw new SuzukiCommonException(ErrorCodeEnum.PAYEE_DEALER_INFO_ERROR);
        }
        /**3、记录订单资料修改**/
        log.info("开始记录订单资料修改资料...");
        modifyService.createModify(EntityTypeEnum.ORDER.getCode(), submitTaskBO.getOrderNo(), logedUser.getStaffId());
        log.info("记录订单资料修改资料完成...");
        /**4、校验同一笔订单中是否存在相同的申请**/
        Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.WITHDRAW.getCode());
        if (delFlag) {
            log.info("当前订单符合贷款申请要求,可以发起贷款申请");
            /**不存在相同申请的订单，直接发起贷款申请**/
            if (!OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                //订单状态无效，无法发起申请
                throw new SuzukiCommonException(ErrorCodeEnum.APPLYSTATUSERROR);
            }
            /**如果订单状态为"征信结果已录入",可发起贷款申请**/
            log.info("正在发起贷款申请...");
            //发起贷款申请
            Boolean startFlag = applyService.startApply(submitTaskBO, logedUser);
            if (startFlag) {
                /**工作流执行成功,更改订单状态**/
                orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.UNDER_REVIEW.getCode());
                return true;
            }
            //贷款申请错误
            throw new SuzukiCommonException(ErrorCodeEnum.LOANAPPLY_ERROR);
        } else {
            /**存在相同的订单，判断是否可以重新发起申请**/
            if (orderStatus == OrderStatusEnum.APPLICATION_IN_NOT_APPROVED.getCode() || orderStatus == OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode()) {
                /**当订单状态为"贷款审核未通过" 或者 "征信结果已录入",可发起重新申请,提交流程**/
                log.info("正在重新发起贷款申请...");
                this.resetLoanApply(submitTaskBO, logedUser);
                return true;
            }
            //存在相同订单的申请，无法再次发起申请
            throw new SuzukiCommonException(ErrorCodeEnum.EXISTSAMEAPPLY);
        }
    }


    /**
     * 业务员发起打款申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public WanderResultVO salesManPayMentApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**1、校验飘红规则**/
        WanderResultVO wanderResultVO = applyService.checkWanderRules(logedUser);
        if (wanderResultVO.getIsCheckWanderRules()) {
            log.info("通过飘红规则验证...");
            /*2、查询订单状态*/
            int orderStatus = orderService.selectOrderStatus(submitTaskBO.getOrderNo());
            /*3、校验是否可以发起打款申请*/
            //返回gps安装状态
            Boolean isApplyFlag = applyService.isPayMentApply(submitTaskBO, logedUser);
            submitTaskBO.setNeedGps(isApplyFlag);
            /*4、校验同一笔订单中是否存在相同的申请*/
            Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.WITHDRAW.getCode());
            if (delFlag) {
                if (orderStatus == OrderStatusEnum.APPLICATION_IS_APPROVED.getCode()) {
                /*如果订单状态为"贷款申请通过",可发起打款申请*/
                    log.info("正在发起打款申请...");
                    Boolean startFlag = applyService.startApply(submitTaskBO, logedUser);
                    if (startFlag) {
                        log.info("发起打款申请成功...");
                        paymentRequestService.savePaymentRequest(submitTaskBO.getOrderNo(), logedUser);
                        orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.PAYMENT_REQUEST.getCode());
                        wanderResultVO.setIsPayMentApply(true);
                        return wanderResultVO;
                    }
                    //打款申请发起失败
                    throw new SuzukiCommonException(ErrorCodeEnum.PAYMENTAPPLY_ERROR);
                }
                //订单状态无效，无法发起申请
                throw new SuzukiCommonException(ErrorCodeEnum.APPLYSTATUSERROR);
            } else {
                /*存在相同的订单，判断是否可以重新发起申请*/
                if (orderStatus == OrderStatusEnum.PAYMENT_REQUEST_REJECT.getCode()) {
                    log.info("重新发起打款申请...");
                /*当订单状态为"贷款审核未通过",可发起重新申请,提交流程*/
                    this.resetLoanApply(submitTaskBO, logedUser);
                    wanderResultVO.setIsPayMentApply(true);
                    return wanderResultVO;
                }
                //存在相同订单的申请，无法再次发起申请
                throw new SuzukiCommonException(ErrorCodeEnum.EXISTSAMEAPPLY);
            }
        } else {
            log.info("飘红规则验证验证失败...");
            return wanderResultVO;
        }
    }


    /**
     * 业务员补录资料
     * 包括GPS信息和附件资料
     *
     * @param gpsInfoBO
     * @param logedUser
     * @throws SuzukiCommonException
     */
    @Override
    public void updateGPSInfo(GpsInfoBO gpsInfoBO, User logedUser) throws SuzukiCommonException {
        log.info("开始补录GPS资料...");
        /**更新当前订单车辆信息的GPS信息**/
        log.info("当前GPS编号为：{},正在更新GPS信息...", gpsInfoBO.getGpsNum());
        CarInfoDO carInfoRecord = new CarInfoDO();
        CarInfoQuery carInfoQuery = new CarInfoQuery();
        //GPS编号
        carInfoRecord.setGpsNum(gpsInfoBO.getGpsNum());
        //gps安装位置
        carInfoRecord.setGpsSetLocation(gpsInfoBO.getGpsSetLocation());
        //订单编号
        carInfoQuery.createCriteria().andOrderNoEqualTo(gpsInfoBO.getOrderNo());
        if (carInfoManager.updateByQuerySelective(carInfoRecord, carInfoQuery) != 1) {
            //补录资料错误
            throw new SuzukiCommonException(ErrorCodeEnum.FILL_INFOMATION_ERROR);
        } else {
            log.info("GPS信息更新完成...");
        }
    }


    /**
     * 查询补录资料信息
     *
     * @param orderNo
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public GpsInfoBO selectGPSInfo(String orderNo) throws SuzukiCommonException {
        log.info("正在获取GPS信息...");
        CarInfoQuery carInfoQuery = new CarInfoQuery();
        carInfoQuery.createCriteria().andOrderNoEqualTo(orderNo);
        /**根据订单号查询车辆信息,获取GPS信息**/
        List<CarInfoDO> carInfoList = carInfoManager.selectByQuery(carInfoQuery);
        if (!CollectionUtils.isEmpty(carInfoList)) {
            GpsInfoBO gpsInfoBO = new GpsInfoBO();
            for (int i = 0; i < carInfoList.size(); i++) {
                gpsInfoBO.setGpsNum(carInfoList.get(i).getGpsNum());
                gpsInfoBO.setGpsSetLocation(carInfoList.get(i).getGpsSetLocation());
            }
            log.info("获取GPS信息完成...");
            return gpsInfoBO;
        }
        //查询错误
        throw new SuzukiCommonException(ErrorCodeEnum.SELECT_FAILURE);
    }


    /**
     * 业务员发起修改申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean updateApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**1、查询订单状态**/
        int orderStatus = orderService.selectOrderStatus(submitTaskBO.getOrderNo());
        /**2、判断是垫款前还是垫款后**/
        Boolean advancePaymentFlag = paymentNotificationService.isOrderStatuByPayment(submitTaskBO.getOrderNo());
        if (advancePaymentFlag) {
            log.info("当前订单状态处于垫款后...");
            submitTaskBO.setIsCashIn(true);
        } else {
            log.info("当前订单状态处于垫款前...");
            submitTaskBO.setIsCashIn(false);
        }
        /**3、记录订单资料修改**/
        log.info("开始记录订单资料修改资料...");
        modifyService.createModify(EntityTypeEnum.ORDER.getCode(), submitTaskBO.getOrderNo(), logedUser.getStaffId());
        log.info("记录订单资料修改资料完成...");
        /**4、校验同一笔订单中是否存在相同的申请**/
        Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.WITHDRAW.getCode());
        if (Boolean.TRUE.equals(delFlag)) {
            if (OrderStatusEnum.UPDATE_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                /**5、发起修改申请**/
                Boolean startFlag = applyService.startApply(submitTaskBO, logedUser);
                if (startFlag) {
                    orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.UNDER_REVIEW.getCode());
                    return true;
                }
                //修改申请错误
                throw new SuzukiCommonException(ErrorCodeEnum.UPDATEAPPLY_ERROR);
            }
            //订单状态无效，无法发起申请
            throw new SuzukiCommonException(ErrorCodeEnum.APPLYSTATUSERROR);
        } else {
            if (OrderStatusEnum.APPLICATION_IN_NOT_APPROVED.getCode().equals(orderStatus) ||
                    OrderStatusEnum.UPDATE_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                log.info("重新发起修改申请...");
                /**当订单状态为"贷款审核未通过",可发起重新申请,提交流程**/
                this.resetLoanApply(submitTaskBO, logedUser);
                return true;
            }
            //存在相同订单的申请，无法再次发起申请
            throw new SuzukiCommonException(ErrorCodeEnum.EXISTSAMEAPPLY);
        }

    }


    /**
     * 业务员发起通融申请
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean accomApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**1、查询订单状态**/
        int orderStatus = orderService.selectOrderStatus(submitTaskBO.getOrderNo());
        /**2、查询订单的贷款信息，获取贷款金额**/
        log.info("正在查询订单贷款申请...");
        LoanInfoDO loanInfoList = loanInfoManager.selectByOrderNo(submitTaskBO.getOrderNo());
        if (loanInfoList != null) {
            submitTaskBO.setLoanMoney(loanInfoList.getLoanAmount().toString());
        }
        /**3、校验同一笔订单中是否存在相同的申请**/
        Boolean delFlag = orderAuditTaskService.closedTask(submitTaskBO, logedUser, ApplyStatusEnum.CANCEL.getCode());
        if (delFlag) {
            if (OrderStatusEnum.APPLICATION_IN_NOT_APPROVED.getCode().equals(orderStatus) || OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                /**当订单状态为"贷款审核未通过"时，可发起通融申请**/
                log.info("正在发起通融申请...");
                Boolean startFlag = applyService.startApply(submitTaskBO, logedUser);
                if (startFlag) {
                    orderService.changeOrderStatus(submitTaskBO.getOrderNo(), OrderStatusEnum.UNDER_REVIEW.getCode());
                    return true;
                }
                //通融申请发起失败
                throw new SuzukiCommonException(ErrorCodeEnum.ACCOMAPPLY_ERROR);
            }
            //订单状态无效，无法发起申请
            throw new SuzukiCommonException(ErrorCodeEnum.APPLYSTATUSERROR);
        } else {
            if (OrderStatusEnum.APPLICATION_IN_NOT_APPROVED.getCode().equals(orderStatus) || OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                log.info("重新发起通融申请...");
                log.info("正在更新通融原因...");
                OrderAuditApplyDO record = new OrderAuditApplyDO();
                OrderAuditApplyQuery query = new OrderAuditApplyQuery();
                record.setRemark(submitTaskBO.getRemark());
                query.createCriteria().andOrderNoEqualTo(submitTaskBO.getOrderNo()).andApplyStatusEqualTo(ApplyStatusEnum.NORMAL.getCode());
                orderAuditApplyManager.updateByQuerySelective(record, query);
                log.info("更新通融原因完成...");
                /**当订单状态为"贷款审核未通过",可发起重新申请,提交流程**/
                this.resetLoanApply(submitTaskBO, logedUser);
                return true;
            }
            //存在相同订单的申请，无法再次发起申请
            throw new SuzukiCommonException(ErrorCodeEnum.EXISTSAMEAPPLY);
        }
    }


    /**
     * 业务员重新发起申请(正常贷款申请、修改、通融、打款)
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    public void resetLoanApply(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        /**根据订单号查询最新的一条申请记录**/
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("orderNo", submitTaskBO.getOrderNo());
        //正常
        paramMap.put("applyStatus", ApplyStatusEnum.NORMAL.getCode());
        //未处理
        paramMap.put("taskStatus", TaskStatusEnum.PENDING.getCode());
        OrderAuditApplyBO orderAuditApplyInfo = orderAuditApplyExtMapper.queryTaskListByOrderNo(paramMap);
        if (orderAuditApplyInfo != null) {
            log.info("正在组装必要元素...");
            submitTaskBO.setApplyId(orderAuditApplyInfo.getApplyId());
            submitTaskBO.setTaskId(orderAuditApplyInfo.getTaskId());
            submitTaskBO.setFlowNodeId(orderAuditApplyInfo.getFlowNodeId());
            //默认为通过
            submitTaskBO.setApprovalResult(ApprovalResultEnum.PASS.getCode());
            Boolean startFlag = applyService.saveOrderAudit(submitTaskBO, logedUser);
            if (startFlag) {
                OrderDO record = new OrderDO();
                OrderQuery query = new OrderQuery();
                query.createCriteria().andOrderNoEqualTo(submitTaskBO.getOrderNo());
                if (ApplyTypeEnum.PAYMENT_REQUEST_APPLY.getCode().equals(submitTaskBO.getApplyType())) {
                    //垫款审核中
                    record.setOrderStatus(OrderStatusEnum.PAYMENT_REQUEST.getCode());
                } else {
                    //订单状态为审核中
                    record.setOrderStatus(OrderStatusEnum.UNDER_REVIEW.getCode());
                }
                /**成功发起申请，修改订单状态**/
                orderManager.updateByQuerySelective(record, query);
            } else {
                //重新发起申请发起失败
                throw new SuzukiCommonException(ErrorCodeEnum.RESETLOANAPPLY_ERROR);
            }

        } else {
            //查询订单申请信息失败
            throw new SuzukiCommonException(ErrorCodeEnum.SELECT_FAILURE);
        }

    }


    /**
     * 资产保全业务员、内勤提交审核
     *
     * @param submitTaskBO
     * @param logedUser
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean assetsSubmitTask(SubmitTaskBO submitTaskBO, User logedUser) throws SuzukiCommonException {
        //更新车辆GPS厂家信息
        if (submitTaskBO.getGpsFactory() != null) {
            log.info("当前为资产保全审核...");
            log.info("正在更新车辆GPS厂家信息...");
            CarInfoQuery carInfoQuery = new CarInfoQuery();
            CarInfoDO carInfoDO = new CarInfoDO();
            carInfoQuery.createCriteria().andOrderNoEqualTo(submitTaskBO.getOrderNo());
            carInfoDO.setGpsFactoryId(submitTaskBO.getGpsFactoryId());
            carInfoDO.setGpsFactory(submitTaskBO.getGpsFactory());
            carInfoManager.updateByQuerySelective(carInfoDO, carInfoQuery);
            log.info("更新完成...");
        } else {
            log.info("当前为内勤审核...");
        }
        return applyService.saveOrderAudit(submitTaskBO, logedUser);
    }


    /**
     * 检查是否显示通融申请按钮
     *
     * @param orderNo
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean isShowAccomApply(String orderNo) throws SuzukiCommonException {
        /**1、根据订单号查询最新的申请记录**/
        log.info("查询当前订单处于的申请类型...");
        OrderAuditApplyQuery query = new OrderAuditApplyQuery();
        query.createCriteria().andOrderNoEqualTo(orderNo).andApplyStatusEqualTo(ApplyStatusEnum.NORMAL.getCode());
        List<OrderAuditApplyDO> orderAuditApplyList = orderAuditApplyManager.selectByQuery(query);
        if (CollectionUtils.isEmpty(orderAuditApplyList)) {
            return false;
        }
        /**2、已发起过申请**/
        int applyType = orderAuditApplyList.get(0).getApplyType();
        log.info("当前申请类型为：{}", ApplyTypeEnum.getApplyTypeEnum(applyType).getName());
        /**查询当前订单的状态**/
        int orderStatus = orderService.selectOrderStatus(orderNo);
        /**当处于贷款申请,通融申请时**/
        if (ApplyTypeEnum.LOAN_APPLY.getCode().equals(applyType) || ApplyTypeEnum.ACCOM_APPLY.getCode().equals(applyType)) {
            /**订单状态处于贷款审核未通过,征信结果已录入**/
            if (OrderStatusEnum.APPLICATION_IN_NOT_APPROVED.getCode().equals(orderStatus) || OrderStatusEnum.CREDIT_HAS_BEEN_RECORDER.getCode().equals(orderStatus)) {
                return true;
            }
        }
        return false;
    }

}
